<?php

	// +----------------------------------------------------------------------
	// | 添加修改商品类目
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\event\store;

	use app\common\event\Base;

	use app\model\store\GoodAttribute as GoodAttributeModel;
	use app\model\store\GoodAttributeGroup as GoodAttributeGroupModel;
	use app\model\ucenter\User as UserModel;
	use think\Exception;

	class GoodAttribute extends Base
	{

		/**
		 * 更新编辑分组
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function group($param, $user = null)
		{
			try {
				if (empty($param)) {
					throw new Exception(Lang::get('__data_format_error_'));
				}
				if (!empty($param['id'])) {
					// 更新操作
					$result = GoodAttributeGroupModel::update($param, ['id' => $param['id']]);
				} else { // 新增
					$result = GoodAttributeGroupModel::create($param);
				}
				$data['code'] = 0;
				$data['data'] = $result;
				$data['message'] = '操作成功';
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 更新编辑分组
		 * @param string $good_category
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function getOptionsByGoodCategory($good_category)
		{
			try {

				$good_category = \app\model\store\GoodCategory::where('id',$good_category)->find();
				if (!$good_category)
				{
					throw new Exception('分类不存在');
				}
				if (!$good_category['attribute_groups'])
				{
					// 查询上级
					$good_category =  \app\model\store\GoodCategory::where('id',$good_category['parent_id'])->find();
					if ($good_category && !$good_category['attribute_groups'])
					{
						$good_category =  \app\model\store\GoodCategory::where('id',$good_category['parent_id'])->find();
					}
				}
				if (!empty($good_category['attribute_groups']))
				{
					$GoodAttributes = GoodAttributeModel::where('good_attribute_group_id','in',$good_category['attribute_groups'])->select();
				} else{
					$GoodAttributes = [];
				}
				$data['code'] = 0;
				$data['data'] = $GoodAttributes;
				$data['message'] = '操作成功';
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 更新编辑分组
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function item($param, $user = null)
		{
			try {

				if (empty($param)) {
					throw new Exception(Lang::get('__data_format_error_'));
				}

				$param['value_format'] = preg_split("/,|，|\r\n|\r|\n/", $param['value_list']);
				$param['value_list'] = implode(",", $param['value_format']);

				if (!empty($param['id'])) {
					// 更新操作
					$result = GoodAttributeModel::update($param, ['id' => $param['id']]);
				} else { // 新增
					$result = GoodAttributeModel::create($param);
				}
				$data['code'] = 0;
				$data['data'] = $result;
				$data['message'] = '操作成功';
			} catch (\Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		/**
		 * 删除数据
		 * @param array $param
		 * @param UserModel $user
		 * @return mixed
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/6/30 11:35
		 */
		public function del($param, $user = null)
		{
			try {

				$del = GoodAttributeModel::where('id', 'in', explode(',', $param['id']))->select();

				foreach ($del as $item) {
					$item['status'] = -2;
					$item->save();
					if (2 == $item['level']) {
						GoodAttributeModel::where('parent_id', $item['id'])->setField('status', -2);
					} else {
						$items = GoodAttributeModel::where('parent_id', $item['id'])->where('status', 1)->select();
						foreach ($items as $item2) {
							$item2['status'] = -2;
							$item2->save();
							GoodAttributeModel::where('parent_id', $item2['id'])->setField('status', -2);
						}
					}
				}
				// 第1步 检测禁止发布的情况
				$data['code'] = 0;
				$data['message'] = '删除成功';
			} catch (Exception $e) {
				$data['code'] = 1;
				$data['message'] = $e->getMessage();
			}
			return $data;
		}

		public static function getOptions()
		{
			$level1 = GoodAttributeModel::where('status', 1)->where('parent_id', 0)->field('id,title,parent_id as fid')->select();

			$result = [];
			foreach ($level1 as $key1 => $item) {
				$children = GoodAttributeModel::where('status', 1)->where('parent_id', $item['id'])->field('id,title,parent_id as fid')->select();

				$first = $item->toArray();
				$first['children'] = $children->toArray();
//				foreach ($children as $key2 => $child) {
//					$children2 = GoodAttributeModel::where('status', 1)->where('parent_id', $child['id'])->field('id,title,parent_id as fid')->select();
//					if (count($children2)) {
//						// 必须三级分类
//						$secend = $child->toArray();
//						$secend['children'] = $children2->toArray();
//						$first['children'][] = $secend;
//					}
//				}
				if (isset($first['children'])) {
					$result[] = $first;
				}
			}
			return $result;
		}

	}
